﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GISCore.Geometry
{
    [Serializable]
    public class PostPolygon
    {
        private Polygon _polygon;
        private LinkedPoint _head;

        public Polygon getPolygon()
        {
            return _polygon;
        }
        public void setPolygon(Polygon polygon)
        {
            this._polygon = polygon;
        }

        public PostPolygon()
        {
        }
        public PostPolygon(LinkedPoint head)
        {
            this._head = head;
        }

        public LinkedPoint getHead()
        {
            return _head;
        }

        public void setHead(LinkedPoint head)
        {
            this._head = head;
        }
        public override string ToString()
        {
            String str;
            LinkedPoint next;
            str = _head.getPoint().ToString();
            next = _head.getNext();
            while (next.getPoint() != _head.getPoint())
            {
                str = str + "-->" + next.getPoint().ToString();
                next = next.getNext();
            }
            return str;
        }
        /// <summary>
        /// Convert a postPolygon object to a polygon 
        /// </summary>
        /// <returns></returns>
        public Polygon Convert(int layerId)
        {
            LinkedPoint next = _head;
            List <Line> lines=new List<Line>();

            do
            {
                Line newLine = new Line(next.getPoint(), next.getNext().getPoint());
                lines.Add(newLine);
                next = next.getNext();
            } while (next != _head);
            Polygon newPoly = new Polygon(layerId, lines);
            return newPoly;
        }
    }
}
